<?php
/**
 * @author spenceryg
 * @date 2022/4/18
 * @time 2:46 下午
 * @focus love & life
 */


namespace app\common\lib\mysql;


class TableModify
{
    protected $table_name = '';
    protected $new_table_name = '';
    protected $sql = '';

    public function __construct(string $table_name)
    {
        $this->table_name = $table_name;
    }


    /**
     * add
     * @param string $filed_name
     * @param string $field_type
     * @param null $default
     * @param null $filed_length
     * @param string $comment
     * @param bool $is_null
     * @return $this
     */
    public function add(string $filed_name, string $field_type,  $default = null,  $filed_length = null,  string $comment = '', bool $is_null = true ): TableModify
    {
        $sql_pre = "alter table {$this->table_name} add ";
        $sql_base =  $this->getSqlbase($filed_name, $field_type, $default, $filed_length, $comment, $is_null);
        $sql = $sql_pre . $sql_base;
        $this->sql = $sql;
        return $this;
    }

    /**
     * delete
     * @param string $field_name
     * @return $this
     */
    public function delete(string $field_name): TableModify
    {
        $sql = "alter table {$this->table_name} drop column {$field_name};";
        $this->sql = $sql;
        return $this;
    }

    /**
     * truncate
     * @return $this
     */
    public function truncate(): TableModify
    {
        $sql = "truncate table {$this->table_name};";
        $this->sql = $sql;
        return $this;
    }

    /**
     * modify
     * @param string $origin_field
     * @param string $filed_name
     * @param string $field_type
     * @param mixed $default
     * @param mixed $filed_length
     * @param string $comment
     * @param bool $is_null
     * @return $this
     */
    public function modify(string $origin_field,string $filed_name, string $field_type, $default = null,  $filed_length = null,  string $comment = '', bool $is_null = true): TableModify
    {
        $sql_pre = "alter table {$this->table_name} change `{$origin_field}` ";
        $sql_base =  $this->getSqlbase($filed_name, $field_type, $default,$filed_length, $comment, $is_null);
        $sql = $sql_pre . $sql_base;
        $this->sql = $sql;
        return $this;
    }

    /**
     * sqlbase
     * @param string $filed_name
     * @param string $field_type
     * @param mixed $default
     * @param mixed $filed_length
     * @param string $comment
     * @param bool $is_null
     * @return string
     */
    protected function getSqlbase(string $filed_name, string $field_type, $default = null, $filed_length = null,  string $comment = '', bool $is_null = true): string
    {
        $filed_length =  $filed_length ? '(' . $filed_length .')' : $filed_length;
        $is_null = $is_null ? 'null' : 'not null';
        if (is_string($default)  && !$default && $default!=='0') {
            $default = "default ''";
        } elseif (is_null($default)) {
            $default = "default null";
            $is_null = '';
        } else {
            $default = "default '{$default}'";
        }
        $comment = $comment ? "comment '{$comment}'": $comment;
        return  "`{$filed_name}` {$field_type}{$filed_length} {$default} {$is_null} {$comment};";
    }

    /**
     * getSql
     * @return string
     */
    public function getSql(): string
    {
        return $this->sql;
    }


    /**
     * createTable
     * @param string $comment
     * @return TableModify
     */
    public function createTable(string $comment): TableModify
    {
        $this->sql = "create table {$this->table_name} (id int(11) primary key auto_increment not null) comment '{$comment}';";
        return $this;
    }

    /**
     * renameTable
     * @param string $new_table_name
     * @param string $comment
     * @return $this
     */
    public function alterTable(string $new_table_name, string $comment = ''): TableModify
    {
        $this->new_table_name = $new_table_name;
        if ($comment) {
            $this->sql =  "alter table {$this->table_name} comment '{$comment}';";
        }
        $this->sql .= "\r\n" .  "rename table {$this->table_name} to {$this->new_table_name};";
        return $this;
    }

    /**
     * dropTable
     * @return $this
     */
    public function dropTable(): TableModify
    {
        $this->sql = "drop table {$this->table_name};";
        return $this;
    }

}
